package com.shujia.wyh.frienddemo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

class FriendMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    /**
     * tom hello hadoop cat
     * <p>
     * <tom-hello,1>
     * <tom-hadoop,1>
     * <tom-cat,1>
     * <hello-hadoop,-1>
     * <hello-cat,-1>
     * <hadoop-cat,-1>
     * <p>
     * <p>
     * hadoop tom hive world hello
     * <hadoop-tom,1>
     * <hadoop-hive,1>
     * <hadoop-world,1>
     * <hadoop-hello,1>
     * ..
     */
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        //将Text类型转换成java中的String类型
        //tom hello hadoop cat
        String line = value.toString();
        //根据空格进行切分
        String[] friends = line.split(" ");
        String yonghu = friends[0];

        for (int i = 1; i < friends.length; i++) {
            context.write(new Text(myFriends(friends[0], friends[i])), new LongWritable(1L));
            for (int j = i + 1; j < friends.length; j++) {
                context.write(new Text(myFriends(friends[i], friends[j])), new LongWritable(-1L));
            }
        }

    }


    //编写一个方法，比较字符串的大小
    public String myFriends(String f1, String f2) {
        int i = f1.compareTo(f2);
        if (i < 0) {
            return f2 + "-" + f1;
        } else {
            return f1 + "-" + f2;
        }
    }
}

//map输出的结果经过环形缓冲区，进行分区，快速排序，归并排序，等待reduce的拉取
//<tom-hello,1> ，<tom-hello,-1>
//<tom-hello,[1,-1]>
class FriendReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {

        //定义一个标志位
        boolean flag = false; //我们给定含义：false代表的是间接好友，true代表直接好友

        for (LongWritable value : values) {
            if (value.get() == 1) {
                flag = true;
                break;
            }
        }

        if (flag) {
            context.write(key, new LongWritable(1L));
        }else {
            context.write(key, new LongWritable(-1L));
        }
    }
}


public class FriendTuiJianDemo {
    public static void main(String[] args) throws Exception {
        //获取hadoop的环境配置
        Configuration conf = new Configuration();

        //创建Job作业
        Job job = Job.getInstance(conf);

        //设置作业名字
        job.setJobName("分析直接好友或者间接好友");

        //设置job的主类
        job.setJarByClass(FriendTuiJianDemo.class);

        //设置reduce的个数，默认是1个reduce
        job.setNumReduceTasks(1);

        //设置Map类
        job.setMapperClass(FriendMapper.class);
        //设置map任务输出的key-value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //设置Reduce类
        job.setReducerClass(FriendReducer.class);
        //设置reduce的输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //设置数据的输入路径
        FileInputFormat.addInputPath(job, new Path(args[0]));
        //设置结果的输出路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //将job作业提交到yarn上执行
        job.waitForCompletion(true);
    }
}
